package aex.service;



import java.util.LinkedHashMap;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import aex.dao.PuserDao;
import aex.domain.Puser;
import aex.domain.SystemAdmin;
import aex.util.PageBean;



@Transactional(readOnly = false)
@Service(PuserService.SERVICE_NAME)

public class PuserService  {

	
	public static final String SERVICE_NAME="aex.service.PuserService";
	/**
	 * 以注解的形式让spring为我们注入dao的实现类
	 */
	@Resource(name = PuserDao.SERVICE_NAME)
	public PuserDao dao;
	
	public PuserService() {
	}

	
	
	



	public void save(Puser obj){
		dao.save(obj);
	}
	
	public Puser findById(int puId) {
		// TODO Auto-generated method stub
		return dao.findObjectByID(puId);
	}



	public void update(Puser puser) {
		dao.merge(puser);
	}



	public void deleteById(int puId) {
		dao.deleteObjectByIDs(puId);
	}



	public Puser check(Puser puser) {
		List<Puser> list=dao.findCollectionByConditionNoPage(" and tName.puAccount='"+puser.getPuAccount()+"'"
				+ "and tName.puPassword='"+puser.getPuPassword()+"'", null, null);
		 if(list.size()>0){
			 return list.get(0);
		 }
		 return null;
	}

	public PageBean<Puser> findPuserByState(PageBean<Puser> pageBean,
			String state) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.puTime", "desc");
		return dao.findPageBean("and tName.puState = '"+state+"'", null, orderby, pageBean);
	}
	
	
	public PageBean<Puser> serchPuser(String keyWord,PageBean<Puser> pageBean) {
			LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
			orderby.put("tName.puTime", "desc");
			if(keyWord!=null&&!keyWord.equals("")){
				
				return dao.findPageBean("and tName.puAccount like '%"+keyWord+"%' or tName.puName like '%"+keyWord+"%'", null, orderby, pageBean);
			}else{
				return dao.findPageBean(" ", null, orderby, pageBean);
			}
	}

	public Puser checkExet(Puser user) {
		List<Puser> list=dao.findCollectionByConditionNoPage("and (tName.puEmail='"+user.getPuEmail()+"'  or tName.puAccount='"+user.getPuAccount()+"')", null, null);
		 if(list.size()>0){
			 return list.get(0);
		 }
		 return null;
	}







	public Puser findByEmail(String email) {
		List<Puser> list=dao.findCollectionByConditionNoPage(" and tName.puEmail='"+email+"'", null, null);
		 if(list.size()>0){
			 return list.get(0);
		 }
		 return null;
	}







	public List<Puser> findByKeyWord(String keyWord) {
		LinkedHashMap<String, String> orderby=new LinkedHashMap<String, String>();
		orderby.put("tName.puTime", "desc");
		return dao.findCollectionByConditionNoPage("and tName.puName like '%"+keyWord+"%'", null, orderby);
	}
}
